<?php
/**
 * 商户类
 * @author 孙银川
 * @date   2017.12.20
 */

namespace app\common\model;
use think\Model;

class Shop extends Model{

    protected $pk ='sp_id';

	/**
     * 获取所有的商户分类
     */
	public function cates($map=[]){
		$data = db('types')->where($map)->select();
		return $data;
	}

	/**
     * 商户分类详情
     */
	public function cate($id){
		$data = db('types')->where('ty_id='.$id)->find();
		return $data;
	}

	/**
     * 添加商户分类
     */
	public function add_cate($data){
		$res = db('types')->insert($data);
		return $res;
	}

	/**
     * 修改商户分类
     */
	public function upd_cate($data){
		$res = db('types')->update($data);
		return $res;
	}

	/**
     * 添加数据
     */
	public function add($data){
		$res = db('shops')->insert($data);
		return $res;
	}

	/**
     * 查询数据 num分页数,map查询条件
     */
	public function enquiries($num, $map=[],$param=[]){
		// 关联代理商表、商户分类、区域表
		$data = db('shops')
			->alias('s')
			->leftJoin('types t','s.sp_tyid=t.ty_id')
			->leftJoin('area a','s.sp_area_id=a.id')
			->leftJoin('agent_manage ag','s.sp_agem_id=ag.agem_id')
			->field('s.sp_id,s.sp_name,s.sp_mobile,s.sp_username,s.sp_true_name,s.sp_phone,a.merger_name,t.ty_name,ag.agem_principals,s.sp_status,s.sp_appid,s.sp_openid,s.sp_login_time,s.sp_create_time')
			->where($map)
			->paginate($num,false,['type' => 'bootstrap','path' => url('Shop/shopList',$param)]
			);
		// 分页
		$data->page = $data->render();
		// 统计
		$data->count = db('shops')->alias('s')->where($map)->count();
		return $data;
	}

	/**
     * 商户提现记录
     */
	public function cash($num, $map=[]){
		// 关联提现表
		$data = db('cash_with')
			->alias('c')
			->join('shops s','s.sp_id=c.shop_id')
			->field('c.*,s.sp_name')
			->where('c.label=2')
			->where($map)
			->paginate($num);
		// 分页
		$data->page = $data->render();
		// 统计
		$data->count = db('cash_with')
					->alias('c')
					->join('shops s','s.sp_id=c.shop_id')
					->field('c.c_id')
					->where('c.label=2')
					->where($map)
					->count();

		return $data;
	}

	/**
     * 数据详情
     */
	public function detail($id){
		// 关联代理商表、区域表
		$data = db('shops')
			->alias('s')
			->leftJoin('types t','s.sp_tyid=t.ty_id')
			->leftJoin('area a','s.sp_area_id=a.id')
			->leftJoin('agent_manage ag','s.sp_agem_id=ag.agem_id')
			->field('s.*,a.merger_name,ag.agem_id,t.ty_name,ag.agem_principals')
			->where('s.sp_id='.$id)
			->find();
		return $data;
	}

	/**
     * 修改数据
     */
	public function edit($data){
		$res = db('shops')->update($data);
		return $res;
	}

	/**
     * 获取所有的代理商
     */
	public function agent(){
		$agent = db('agent_manage')->field('agem_id,agem_principals')->select();
		return $agent;
	}

	/**
     * 生成唯一商户编号
     */
	public function shopNum(){
		$number = 'rbw'.date('YmdHis').mt_rand(1,9999);
		return $number;
	}

	/**
     * 获取分类下的商户
     */
	public function cate_shops($ty_id){
		$data = db('shops')->field('sp_id,sp_name,sp_logo_url')->where('sp_tyid='.$ty_id.' and sp_audit=1 and sp_status=1')->select();
		return $data;
	}

    /**
     * 获取经纬度
     * @return mixed
     */
    public function getjw($address='',$key='4M6BZ-4F6HG-MUSQI-ISCYF-43DFV-27FNV'){
		$newaddress = str_replace(" ","",$address);
		$arr = json_decode(file_get_contents('http://apis.map.qq.com/ws/geocoder/v1/?address='.$newaddress.'&key='.$key),true);
		if($arr){
			return $arr['result']['location'];
		}else{
			return 0;
		}
    }
}
?>